﻿/*  ----------------------------------------------------------------------------
 *  EcoSqlCondition (http://ecosqlcondition.codeplex.com)
 *  ----------------------------------------------------------------------------
 *  License:    http://ecosqlcondition.codeplex.com/license  
 *  ----------------------------------------------------------------------------
 *  Company:    P3-Studio (http://www.p3studio.pl)
 *  File:       PersistenceMapperSqlCondition.cs
 *  Author:     Paweł Pawłow
 *  ----------------------------------------------------------------------------
 */

using System;
using System.Data.Common;
using System.Drawing;
using Eco.Services;
using System.ComponentModel;

namespace Eco.Persistence
{
	[ToolboxBitmap(typeof(PersistenceMapperSqlCondition), @"Eco.Persistence.PersistenceMapperSqlCondition.bmp")]
	[ToolboxItem(true)]
	public class PersistenceMapperSqlCondition : ChainedPersistenceMapperBase, IPersistenceMapperDb
	{
		public DbConnection DbConnection { get; set; }

		public override ChainedPersistenceMapperImplBase CreatePersistenceMapperImpl(
			ITypeSystemService typeSystemService, 
			IPersistenceMapper nextPersistenceMapper)
		{
			return new PersistenceMapperSqlConditionImpl(typeSystemService, nextPersistenceMapper, PersistenceMapperDb);
		}

		PersistenceMapperDb PersistenceMapperDb
		{
			get
			{
				if (pmDb != null)
					return pmDb;

				PersistenceMapper next = NextPersistenceMapper;
				while (next is ChainedPersistenceMapperBase)
					next = next as ChainedPersistenceMapperBase;

				if (!(next is PersistenceMapperDb))
					throw new InvalidOperationException(@"No PersistenceMapperDb");
				pmDb = (PersistenceMapperDb)next;
				return pmDb;
			}
		}

		PersistenceMapperDb pmDb;

		#region IPersistenceMapperDb Members

		public void CreateDatabaseSchema(ITypeSystemService typeSystemService, IConfigureCleanPS configureCleanPS)
		{
			PersistenceMapperDb.CreateDatabaseSchema(typeSystemService, configureCleanPS);
		}

		public IDBEvolutor GetEvolutor(ITypeSystemService typeSystemService)
		{
			return PersistenceMapperDb.GetEvolutor(typeSystemService);
		}

		public void ReturnEvolutor(IDBEvolutor evolutor)
		{
			PersistenceMapperDb.ReturnEvolutor(evolutor);
		}

		#endregion
	}
}
